<!--
 * @Description: 选择精英等级弹窗
 * @Author: liyujie
 * @Date: 2021/08/31
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/08/31
-->
<template>
    <section class="wrapper">
        <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="640px"
            :close-on-click-modal="false"
            @close="beforeClose"
        >
            <div class="dialog-content">
                <div
                    class="message-view"
                    v-if="message"
                >
                    {{ message }}
                </div>
                <el-checkbox-group v-model="level">
                    <el-checkbox
                        :label="0"
                    >
                        E0(已认证)
                    </el-checkbox>
                    <el-checkbox
                        v-for="item in 5"
                        :key="item"
                        :label="item"
                    >
                        E{{ item }}
                    </el-checkbox>
                </el-checkbox-group>
            </div>
            <div class="dialog-footer">
                <el-button
                    size="medium"
                    type="default"
                    :style="{width: '120px', marginRight: '32px'}"
                    v-if="hasCancelButton"
                    @click="close"
                >
                    {{ cancelButtonText }}
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    :style="{width: '120px'}"
                    :disabled="level.length === 0"
                    @click="handleConfirm"
                >
                    {{ confirmButtonText }}
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import Vue from "vue";

const Dialog = {
    name: 'SelectAgentLevel',
    props: {
        title: {
            type: String,
            default: '提示'
        },
        message: {
            type: String,
            default: ''
        },
        confirmButtonText: {
            type: String,
            default: '确定'
        },
        cancelButtonText: {
            type: String,
            default: '取消'
        },
        hasCancelButton: {
            type: Boolean,
            default: true
        },
        placeholder: {
            type: String,
            default: '请选择...'
        },
    },
    data() {
        return {
            dialogVisible: false,
            level: []
        };
    },
    mounted() {},
    methods: {

        /**
         * 关闭弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * 弹窗关闭前
         */
        beforeClose() {
            this.level = 0;
            this.$emit('close');
        },

        /**
         * 确定
         */
        handleConfirm() {
            this.$emit('confirm', this.level.join());
        }
    },
};

export default Dialog;

const selectAgentLevelDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", () => {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("confirm", (data) => {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

export { selectAgentLevelDialog };
</script>

<style lang="less" scoped>
.wrapper {
    .dialog-content {
        .message-view {
            margin-bottom: 16px;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            &::before {
                content: "*";
                color: #FF3030;
            }
        }
    }
    .dialog-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 24px;
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-dialog__body {
        padding: 32px 64px;
    }

    /deep/ .el-textarea__inner {
        resize: none;
    }
}
</style>
